package com.appsys.controller;

import com.appsys.pojo.Backend;
import com.appsys.pojo.Dev;
import com.appsys.service.BackendService;
import com.appsys.service.DevService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author YinLiangTao Email:1517535831@qq.com
 * @Date 2022/2/25 15:42
 * @Version 1.0
 */

@Controller
public class LoginController {
    @Resource
    private DevService devService;

    @Resource
    private BackendService backendService;
    //跳转用户登入页面
    @RequestMapping(value = "/devlogin")
    public String devLogin(){
        return "devlogin";
    }
    //跳转后台管理员页面
    @RequestMapping(value = "/backendlogin")
    public String backendLogin(){
        return "backend/backendlogin";
    }

//    @GetMapping(value = "/devmain")
//    public String devMain(Model model){
//        model.addAttribute("error","请先登录");
//        return "devlogin";
//    }
    //跳转用户登入成功页面
    @PostMapping(value = "/devmain")
    public String devMain(@RequestParam(value = "devName",required = false) String devName, @RequestParam(value = "devPassword",required = false) String devPassword, Model model, HttpSession session){
        Dev login = devService.login(devName, devPassword);
        if(login != null){
            session.setAttribute("dev",login);
            return "redirect:/devmain.html";
        }else{
            model.addAttribute("error","用户名或密码错误");
            return "forward:/devlogin";
        }
    }
    @RequestMapping(value = "/devmain.html")
    public String toMain(HttpSession session,Model model){
        return "devmain";
    }


    @GetMapping(value = "/backendmain")
    public String backendMain(Model model){
        model.addAttribute("error","请先登录");
        return "backend/backendlogin";
    }

    //跳转管理员登入成功页面
    @PostMapping(value = "/backendmain")
    public String backendMain(@RequestParam(value = "backendName",required = false) String backendName, @RequestParam(value = "backendPassword",required = false) String backendPassword, Model model, HttpSession session){
        Backend login = backendService.login(backendName, backendPassword);
        if(login != null){
            session.setAttribute("backend",login);
            return "redirect:/backendmain.html";
        }else{
            model.addAttribute("error","用户名或密码错误");
            return "forward:/backendlogin";
        }
    }
    @RequestMapping(value = "/backendmain.html")
    public String toBackendMain(HttpSession session,Model model){
        return "backend/backendmain";
    }
    //注销登录
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        if (session.getAttribute("dev") != null){
            session.setAttribute("dev",null);
        }
        return "forward:/";
    }
    //注销登录
    @RequestMapping(value = "/backendlogout")
    public String backendlogout(HttpSession session){
        if (session.getAttribute("backend") != null){
            session.setAttribute("backend",null);
        }
        return "forward:/";
    }

}
